<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Kuyuan\Phpwidget\Str;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Member extends Authenticatable implements JWTSubject
{
    use HasApiTokens;

    protected $fillable = [
        'nickname',
        'avatar',
        'openid',
        'status',
        'balance',
        'red_balance'
    ];

    protected $hidden = [
        'openid'
    ];

    protected $appends = ['format_balance','format_red_balance'];

    /**
     * @inheritDoc
     */
    public function getJWTIdentifier()
    {
        // TODO: Implement getJWTIdentifier() method.
        return $this->getKey();
    }

    /**
     * @inheritDoc
     */
    public function getJWTCustomClaims()
    {
        // TODO: Implement getJWTCustomClaims() method.
        return [];
    }

    /**
     * 格式化用户余额
     * @param $_value
     * @return string
     */
    public function getFormatBalanceAttribute()
    {
        return Str::format_money($this->balance);
    }

    /**
     * 格式化用户红包金额
     * @param $_value
     * @return string
     */
    public function getFormatRedBalanceAttribute()
    {
        return Str::format_money($this->red_balance);
    }

    /**
     * 用户发红包数量
     */
    public function getRedNumberAttribute()
    {
        return Red::where("member_id",$this->id)
            ->where("status","!=",1)
            ->where("status","!=",6)
            ->where("status","!=",8)
            ->count();
    }

    /**
     * 用户注册时间
     */
    public function getRegisterDateAttribute()
    {
        return date('Y-m-d H:i:s',strtotime($this->created_at));
    }
}
